using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using GameFramework.Configuration;
using GameFramework.DynamicText;
using GameFramework.Reference;
using GameFramework.Request;
using GameLogic.Configurations.UI;

namespace GameLogic.UI.Prompt
{
    /// <summary>
    /// 展示提示语请求
    /// </summary>
    public sealed class OpenPromptRequest : ARequest
    {
        /// <summary>
        /// 提示语Id
        /// </summary>
        public int Id { get; set; }
        
        /// <summary>
        /// 提示语内容动态文本参数
        /// </summary>
        private readonly Dictionary<string, string> m_Vars = new Dictionary<string, string>();
        
        public override void OnRecycle()
        {
            Id = 0;
            m_Vars.Clear();
        }

        /// <summary>
        /// 设置文本变量
        /// </summary>
        public OpenPromptRequest SetVar(string name, string value)
        {
            m_Vars[name] = value;
            return this;
        }

        protected override async UniTask<IResponse> OnExecute(CancellationToken cancellationToken)
        {
            var config = ConfigurationService.That.Get<TbPromptSetting>().GetOrDefault(Id);
            
            // 配置表未找到时 代表无需显示提示语 直接返回成功
            if (config == null)
                return ReferenceService.GetReference<CommonResponse>().SetErrorCode(0);
            
            // 指定无需提示时 直接返回成功
            if (config.PromptType == PromptType.None)
                return ReferenceService.GetReference<CommonResponse>().SetErrorCode(0);

            var promptText = config.Content;
            if (!string.IsNullOrEmpty(promptText) && m_Vars.Count > 0)
                promptText = promptText.SetVars(m_Vars).FlushVars();

            switch (config.PromptType)
            {
                case PromptType.MessageBox:
                    // 提示内容为空时 直接返回成功
                    if (string.IsNullOrEmpty(promptText))
                        return ReferenceService.GetReference<CommonResponse>().SetErrorCode(0);
                    
                    // TODO 展示MessageBox
                    break;
                case PromptType.Toast:
                    // 提示内容为空时 直接返回成功
                    if (string.IsNullOrEmpty(promptText))
                        return ReferenceService.GetReference<CommonResponse>().SetErrorCode(0);
                    
                    // TODO 展示Toast
                    break;
            }
            
            // 不支持的提示类型 直接返回成功
            return ReferenceService.GetReference<CommonResponse>().SetErrorCode(0);
        }

        /// <summary>
        /// 构造展示提示语请求实例
        /// </summary>
        public static OpenPromptRequest Make(int id)
        {
            var request = GameFramework.Reference.ReferenceService.That.GetReference<OpenPromptRequest>();
            request.Id = id;
            return request;
        }
        
        /// <summary>
        /// 展示指定提示语
        /// </summary>
        public static UniTask<int> OpenPrompt(int id, CancellationToken token)
        {
            return Make(id).Execute(token).PickCode();
        }
    }
}